package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.sys.entity.DormAreaEntity;
import io.renren.modules.sys.entity.DormRepairEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.DormAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.DormFareEntity;
import io.renren.modules.sys.service.DormFareService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Lizhiwei
 * @email 123@qq.com
 * @date 2022-10-11 19:43:49
 */
@RestController
@RequestMapping("sys/dormfare")
public class DormFareController extends AbstractController{
    @Autowired
    private DormFareService dormFareService;
    @Autowired
    private DormAreaService dormAreaService;

    /**
     * 缴费
     */
    @RequestMapping("/pay/{id}")
//    @RequiresPermissions("sys:dormfare:list")
    public R pay(@RequestParam Map<String, Object> params,@PathVariable("id") String id){
        DormFareEntity entity = dormFareService.getById(id);
        entity.setStatus(1);
        dormFareService.updateById(entity);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list01/{type}")
//    @RequiresPermissions("sys:dormfare:list")
    public R list01(@RequestParam Map<String, Object> params,@PathVariable("type") String type){
        if (type.equals("self")){
            SysUserEntity user = getUser();
            if (user.getDormId() == null || user.getDormId() == 0) {
                params.put("dormId", 0);
            }else{
                params.put("dormId", user.getDormId());
            }
        }
        PageUtils page = dormFareService.queryPage01(params);

        List<DormFareEntity> list = (List<DormFareEntity>) page.getList();
        List<Object> list1 = list.stream().map((entity) -> {
            if (entity.getDormId() != null) {
                DormAreaEntity byId = dormAreaService.getById(entity.getDormId());
                if (byId!=null && byId.getName()!=null && !byId.getName().equals("")){
                    entity.setDormId(Integer.parseInt(byId.getName()));
                    //获取宿舍楼
                    DormAreaEntity byId1 = dormAreaService.getById(byId.getParentId());
                    entity.setSecondLevel(byId1.getName());
                    //获取宿舍区
                    DormAreaEntity byId2 = dormAreaService.getById(byId1.getParentId());
                    entity.setFirstLevel(byId2.getName());
                }
            }
            return entity;
        }).collect(Collectors.toList());
        page.setList(list1);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:dormfare:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dormFareService.queryPage(params);

        //获取真实宿舍号
        List<DormFareEntity> list = (List<DormFareEntity>) page.getList();
        List<Object> list1 = list.stream().map((entity) -> {
            if (entity.getDormId() != null) {
                DormAreaEntity byId = dormAreaService.getById(entity.getDormId());
                if (byId!=null && byId.getName()!=null && !byId.getName().equals("")){
                    entity.setDormId(Integer.parseInt(byId.getName()));
                    //获取宿舍楼
                    DormAreaEntity byId1 = dormAreaService.getById(byId.getParentId());
                    entity.setSecondLevel(byId1.getName());
                    //获取宿舍区
                    DormAreaEntity byId2 = dormAreaService.getById(byId1.getParentId());
                    entity.setFirstLevel(byId2.getName());
                }
            }
            return entity;
        }).collect(Collectors.toList());
        page.setList(list1);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:dormfare:info")
    public R info(@PathVariable(name = "id") Integer id){
		DormFareEntity dormFare = dormFareService.getById(id);

        return R.ok().put("dormFare", dormFare);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:dormfare:save")
    public R save(@RequestBody DormFareEntity dormFare){
        dormFare.setCreateTime(new Date());
		dormFareService.save(dormFare);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:dormfare:update")
    public R update(@RequestBody DormFareEntity dormFare){
		dormFareService.updateById(dormFare);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:dormfare:delete")
    public R delete(@RequestBody Integer[] ids){
		dormFareService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
